package com.qiantu.phone.ui.fragment.device;

import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceState;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.KHCurtainSeek;
import com.qiantu.phone.widget.StatusSeekBar;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 *    desc   : 梦幻帘设备
 */
public final class BFCurtainFragment extends BaseDeviceFragment {
    private KHCurtainSeek curtainSeek;
//    private VibratorManager mVibratorManager;
    private StatusSeekBar statusSeekBar;

    public static BFCurtainFragment newInstance() {
        BFCurtainFragment fragment = new BFCurtainFragment();
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bf_curtain;
    }

    @Override
    protected void initView() {
        super.initView();
        curtainSeek = findViewById(R.id.curtain_seek);
        statusSeekBar = findViewById(R.id.status_seek_bar);
        curtainSeek.setOnChangeListener(new KHCurtainSeek.OnChangeListener(){
            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onProgressChanged(int value) {

            }

            @Override
            public void onValueChanged(int progress) {
                int strokePercentage = 100 - progress;
//                DeviceState deviceState = mDevice.getStateData();
//                Integer oldStrokePercentage = deviceState.getStatus().getStrokepercentage();
//                if(Float.valueOf(oldStrokePercentage) != strokePercentage){
//                    strokePercentageOperation(strokePercentage);
//                }
                strokePercentageOperation(strokePercentage);
            }
        });
        statusSeekBar.setOnSeekBarChangeListener(new StatusSeekBar.OnSeekBarChangeListener() {
            @Override
            public void onValueChanged(int value) {
            }

            @Override
            public void onStartTrackingTouch() {

            }

            @Override
            public void onStopTrackingTouch(int value) {
                strokeAngleOperation(value);
            }
        });
        updateView();
    }

    /**
     * 调节开合度
     * @param strokePercentage
     */
    private void strokePercentageOperation(int strokePercentage){
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.STROKE_PERCENTAGE, strokePercentage);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                mDevice.setStrokeStatePercentage(strokePercentage);
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                refreshDeviceStateByLocal();
            }

            @Override
            public void onFail(Exception e) {
                curtainSeek.setProgress(100 - mDevice.getStrokePercentage());
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    private void strokeAngleOperation(int strokePercentage){
        Map<String, Object> option = new HashMap<>();
        option.put(LLDeviceUtil.AMGLE, strokePercentage);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
                mDevice.getStateData().getStatus().setAngle(strokePercentage);
                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
                refreshDeviceStateByLocal();
            }

            @Override
            public void onFail(Exception e) {
                curtainSeek.setProgress(100 - mDevice.getStrokePercentage());
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

    protected void updateView(){
        super.updateView();
        DeviceState deviceState = mDevice.getStateData();
        Integer strokePercentage = deviceState.getStatus().getStrokepercentage();
        Float progress = Float.valueOf(strokePercentage);
//        if(mDevice.isIsOnline()){
        curtainSeek.setEnabled(true);
        curtainSeek.setProgress(100 - progress.intValue());
        statusSeekBar.setValue(deviceState.getStatus().getAngle());
//        }else{
//            curtainSeek.setEnabled(false);
//            curtainSeek.setProgress(0);
//        }
//        curtainSeek.setProgress(progress.intValue());
    }
}